<!DOCTYPE HTML>
<html>
	<head>
	<title>个股行情</title>
	<link href="css/text.css" rel="stylesheet" type="text/css" />
	<meta charset="utf-8" />
	<style>
		tr.highlight {
		 background:#08246B;
		 color:white;
		}
		.fixedBox{
				border: 1px solid #555555;				
				white-space: nowrap;	
			}
			.fixedBox::-webkit-scrollbar {
				width: 10px;     
				height: 10px;				
			}			
		table{
			width:600px; 
			border-collapse:collapse; 
			text-align:center;
		}
		td,th{border:1px solid #696969}
	</style>
	<script type="text/javascript" src="js/jquery.min.js"></script>
	<script type="text/javascript" src="DatePicker/WdatePicker.js"> </script>
	</head>
	<body>
		<div id="towrite" class="fixedBox"></div>
		<div id="data"></div>
		<script>
			var bkcolor = $("body").css("backgroundColor");
			var thcolor = "";
			var tdcolor = "";
			var dt = "";
			var tt = "";
			var gt = "";
			var k = "0";
			window.onload = function() {
				var day = new Date();
				gt = day.getTime();
				mymonth = day.getMonth()+1;
				myday = day.getDate();
				if(mymonth >= 10){mymonth = mymonth;}else{mymonth = "0" + mymonth;}
				if(myday >= 10){myday = myday;}else{myday = "0" + myday;}
				tt = day.getFullYear() + "-" + mymonth + "-" + myday;
				dt = tt;				
				changeStyle();
				getSource(6);
			}
						
			function time_range(beginTime, endTime) {
				var strb = beginTime.split(":");
				if (strb.length != 2) {
					return false;
				}

				var stre = endTime.split(":");
				if (stre.length != 2) {
					return false;
				}

				var b = new Date();
				var e = new Date();
				var n = new Date();

				b.setHours(strb[0]);
				b.setMinutes(strb[1]);
				e.setHours(stre[0]);
				e.setMinutes(stre[1]);

				if (n.getTime() - b.getTime() > 0 && n.getTime() - e.getTime() < 0) {
					tk = 1;
					getSource(fs);					
					console.log(true)　　　　　　
					return true;				
				} else {
					console.log(false)
				    return false;     
				}
			}　　　
   　　　
			$(function() {
				setInterval(function() {
				time_range("9:00","15:15")				
				},
				60000);
			})
			
			Date.prototype.Format = function (fmt) {
				var o = {
				"M+": this.getMonth() + 1, //月份
				"d+": this.getDate(), //日
				"h+": this.getHours(), //小时
				"m+": this.getMinutes(), //分
				"s+": this.getSeconds(), //秒
				"q+": Math.floor((this.getMonth() + 3) / 3), //季度
				"S": this.getMilliseconds() //毫秒
				};
				if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
				for (var k in o)
				if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
				return fmt;
			};
			
			function changeStyle() {
				var obj = document.getElementById("towrite");								
				obj.style.left= "0px"; 				
				obj.style.width= "620px";	
				//obj.style.height= "620px";		
				obj.style.fontSize="12px";					
			}
						
			function keepTwoDecimal(num) {  
               var result = parseFloat(num);  
               if (isNaN(result)) {  
                   //alert('传递参数错误，请检查！');  
                   return false;  
               }  
               result = Math.round(num * 100) / 100;  
               return result;  
			};
			
			function NumberTransform(num) { 
				var result = num;
               if(Math.abs(num) > 100000000){
					result = num / 100000000;
					result = result.toFixed(2) + "亿";
			   }else if(Math.abs(num) > 10000){
					result = num/10000;
					result = result.toFixed(0) + "万";
			   }				   
               return result;  
			};	
			
			var trid = "";
			var trs = "";
			function changeCss(obj,id){	
				trs = "";
				var t = obj.parentNode.parentNode.id;  //td父节点tr id
				var ty = t.split("#")[1];
				var ty1 = "";
				var ty2 = "";
				if(ty.indexOf("、") > -1){
					ty1 = ty.split("、")[0];
					ty2 = ty.split("、")[1];
				}
				
				var tlist = document.getElementById(id).getElementsByTagName("tr");					
				for(var i = 0; i < tlist.length; i++){
					if(tlist[i].id != ""){
						var dm = tlist[i].id.split("#")[0];
						var yy = tlist[i].id.split("#")[1];
						if(t == tlist[i].id){
							tlist[i].style.backgroundColor = "#483D8B";  //选中行
							trid = t;
						}else if(ty.indexOf("、") > -1){
							if(yy.indexOf(ty1) > -1 || yy.indexOf(ty2) > -1){
								tlist[i].style.backgroundColor = "#800080"; //相同涨停原因
								trs += tlist[i].id;
							}else{
								tlist[i].style.backgroundColor = bkcolor; 				
							}	
						}else{
							if(yy.indexOf(ty) > -1){
								tlist[i].style.backgroundColor = "#800080"; //相同涨停原因
								trs += tlist[i].id;
							}else{
								tlist[i].style.backgroundColor = bkcolor; 				
							}	
						}	
					}					
				}				
			}
			
			function hover(){  //鼠标经过
				var rows = document.getElementsByTagName('tr');
				for(var i=0;i<rows.length;i++){
					rows[i].onmouseover=function(){							
						if(this.id != ""){  //过滤表头
							this.style.background="#800000";
						}
					}
					rows[i].onmouseout=function(){						
						if(this.id != ""){  //过滤表头
							if(this.id == trid){  //过滤选中行
								this.style.background="#483D8B";
							}else if(trs.indexOf(this.id) > -1){
								this.style.background="#800080";
							}else{
								this.style.background=bkcolor;
							}
						}						
					}					
				}
			}			
			
			var ts = "";
			var tk = 0;			
			function cDayFunc(dp) 
			{ 
				var date = dp.cal.getNewDateStr();            
				BookingRmk(date);//调用其它函数并带上参数                
			}			
			
			function BookingRmk(date){         
				tt = date;
				var date1 = new Date(tt);
				var date2 = new Date(dt);						
				k = 0;
				if(date1.getTime() < date2.getTime()){			
					k = 1;
					tradeday();
				}
				
				if(ts != tt){			
					tk = 1;
				}				
				getSource(fs);
				ts = tt;
				tk = 0;						
			}

			function tradeday() {  //判断最后交易日
				var day = new Date(tt);							
				for (i = 1; i < 100; i++){	//节假日																										
					if(i > 1){
						day.setTime(day.getTime() - 24*60*60*1000);							
					}	
					tt = day.Format("yyyy-MM-dd");
					//alert(tt);
					var url = "http://127.0.0.1:8881/w1/api/index.php?Order=1&a=HisRankingInfo_W8&st=10&c=HisStockRanking&Isst=0&apiv=w28&Type=6&Date=" + tt;					
					var res = [];
					$.ajax({
						type: "get",
						url: url,
						dataType: "json",
						async : false,
						cache:false,
						timeout:3000,						
						success: function(data) {							
							res = data.list;							
						}
					});
					if(res.length != 0){						
						break;
					}
				}
			}	
			
			var fx = "1";
			var fs = "";
			var gl = "";  //高亮
			function getSource(p) {					
				fs = p;			
				div = document.getElementById('towrite');
				div.innerHTML = '<font size="4">个股行情&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</font><input style="width: 120px;" type="text" id="PriceDate" name="PriceDate" class="Wdate" onFocus="WdatePicker({ onpicking: cDayFunc})" />';						
				
				var url1 = "http://127.0.0.1:8880/w1/api/index.php?Order=" + fx + "&a=RealRankingInfo_W8&st=60&c=NewStockRanking&Isst=0&index=0&apiv=w28&Ratio=6&Type=" + fs;								
				var url2 = "http://127.0.0.1:8881/w1/api/index.php?Order=" + fx + "&a=HisRankingInfo_W8&st=100&c=HisStockRanking&Isst=0&apiv=w28&Ratio=6&Type=" + fs + "&Date=" + tt;

				url = url1;
				if(k == "1" ){
				  url = url2;
				}
				//alert(url)
				
				var res = [];
				$.ajax({
					type: "get",
					url: url,
					dataType: "json",
					async : false,
					cache:false,
					timeout:3000,					
					success: function(data) {
						//alert(JSON.stringify(data))
						//document.write(JSON.stringify(data));	
						if(k == 0){
							tt = data.Day[0];							
							dt = tt;
						}
						for (i = 0; i < data.list.length; i++){
							res.push({"代码":data.list[i][0],"名称":data.list[i][1],"现价":data.list[i][5],"涨幅%":data.list[i][6],"涨速%":keepTwoDecimal(data.list[i][9]),"主力净额":NumberTransform(data.list[i][13]),"板块":data.list[i][4]});							
						}											
					}
				});
				//alert(JSON.stringify(res))				
				writeSource(res)
			}
			
			var bk = 0;
			function writeSource(data) { 				
				var json = data;
				
				//创建table					
				var table=document.createElement("table");										
				var thead=document.createElement("thead");
				table.appendChild(thead);					
				var tr=document.createElement("tr");
				thead.appendChild(tr);					
				
				var m = 0;
				var n = 0;
				var b = 0;
				var gl = "";  //高亮
				if(fs == "10"){
					gl = "现价";
				}else if(fs == "6"){
					gl = "涨幅";
				}else if(fs == "7"){
					gl = "涨速";
				}else if(fs == "1"){
					gl = "主力净额";
				}					
				for(var key in json[0]){  //表头					
					var th=document.createElement("th");						
					th.innerHTML=key;						
					tr.appendChild(th);						
					if(gl == key.replace("%","")){
						n = m;
						th.style.color = "#FF6633";	
						if(fx == 0){
							th.style.color = "#ADFF2F";	
						}
					}
					if(key == "板块"){
						b = m;
						bk = m;
						th.style.color = "#FFA500";	
					}
					m++;
				}
									
				var tbody=document.createElement("tbody");					
				table.appendChild(tbody);
				
				//遍历json
				for(var i = 0; i < json.length; i++){					
					var tr=document.createElement("tr");
					tr.setAttribute("onclick","selectTR(0)");						
					var p = 1;
					var dm = "";
					var mc = "";
					var zt = "";
					for(var key in json[i]){ //行元素遍历						
						var td=document.createElement("td");							
						
						var rc = json[i][key];
						//alert(rc)
						if(p == 1){  //代码列
							dm = rc;								
						}else if(p == 2){  //名称列
							mc = rc;								
						}else if(p == 7){
							zt = rc;
							tr.id = dm + "#" + zt;
						}
												
						td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "' onclick='changeCss(this,\"gg\")'>" + rc + "</a>";
						//td.innerHTML = "<a href=http://www.treeid/code_" + dm + " id='" + p + "'>" + rc + "</a>";
						
						if(n != 0 && p == n + 1){							
							td.getElementsByTagName("a")[0].style.color = "#FF6633";
							if(fx == 0){							
								td.getElementsByTagName("a")[0].style.color = "#ADFF2F";
							}
						}
						if(p == b + 1){							
							td.getElementsByTagName("a")[0].style.color = "#FFA500";
						}																
						tr.appendChild(td);							
						p++;
					}
					if(mc.indexOf("ST") == -1 && dm.substr(0,3) != "688"){		//过滤ST、科创板										
						tbody.appendChild(tr);
					}												
				}					
				var div = document.getElementById("towrite");									
				div.appendChild(table);
				table.id = "gg";
				table.style.marginTop = "10px";
				thcolor = $("th").css("color");				
				tdcolor = $("td a:first").css("color");						
				changeTable(table);		//隐藏某列
				makeSortable(table);
					
				document.getElementById("PriceDate").value = tt;
				hover();
			}
			
			//隐藏某列				
            function changeTable(table) {
                var trs = table.rows;
                for (var i = 0, len = trs.length; i < len; i++) {
                    var cell = trs[i].cells[0]; //隐藏第1列					
                    cell.style.display = 'none';
                }
            }
           
			var pt = 0;
			var rs = 0;
			function makeSortable(table) {
				var headers=table.getElementsByTagName("th");
				for(var i=0;i<headers.length;i++){				
					(function(n){
						var flag=false;
							
						headers[n].onclick=function(){
							//alert(n);	
							
							if(n > 1 && n < 6){
								if(n == 2){
									fs = "10";															
								}else if(n == 3){
									fs = "6";							
								}else if(n == 4){
									fs = "7";							
								}else if(n == 5){
									fs = "1";							
								}
								if(rs != n){  //切换列 清零
									rs = n;
									fx = "1";
									pt = 1;
								}else{
									fx = (pt%2 == 0) ? "1" : "0";
									pt++;
								}
								//alert(fs);
								getSource(fs);							
							}
							
							showCol(n);		//高亮
							var tbody=table.tBodies[0];
							var rows=tbody.getElementsByTagName("tr");
							rows=Array.prototype.slice.call(rows,0);

							//基于第n个<td>元素的值对行排序
							rows.sort(function(row1,row2){
								headtxt = headers[n].textContent;								
								var cell1=row1.getElementsByTagName("td")[n];//获得第n个单元格
								var cell2=row2.getElementsByTagName("td")[n];																
								var val1=cell1.textContent||cell1.innerText;//获得文本内容
								var val2=cell2.textContent||cell2.innerText;
								<!-- if(headtxt.indexOf("名称") != -1 || headtxt.indexOf("板块") != -1){ -->
									<!-- val1 = val1; -->
									<!-- val2 = val2; -->
								<!-- } -->
								if(val1<val2){
									return -1;
								}else if(val1>val2){
									return 1;
								}else{
									return 0;
								}
								
							});
							if(flag){
								rows.reverse();								
							}
							
							for(var i=0;i<rows.length;i++){
								tbody.appendChild(rows[i]);
							}

							flag=!flag;							
						}
					}(i));
				}
			}
			
			function showCol(iCol){			
				var div = document.getElementById("towrite");
				var oTable = div.getElementsByTagName('table')[0];
				for (i=0;i < oTable.rows.length; i++){										
					for (j=0;j < oTable.rows[0].cells.length; j++){
						oTable.rows[i].cells[bk].style.color = "#FFA500";
						if(iCol == bk){
							oTable.rows[i].cells[j].style.color = thcolor;
							oTable.rows[i].cells[iCol].style.color = "#FF6633";	
						}													
					}							
				}

				otagsA = oTable.getElementsByTagName("a");  //a变色
				for (i = 0; i < otagsA.length; i++) {
					if(iCol == bk){
						otagsA[i].style.color = tdcolor;
						if(otagsA[i].id == bk + 1){
							otagsA[i].style.color = "#FF6633";
						}
					}else{
						if(otagsA[i].id == bk + 1){
							otagsA[i].style.color = "#FFA500";
						}
					}
				}								
			}
		</script>		
	</body>
</html>